package com.yitao.cms.service.product;


import com.skipper.base.bean.DataOutput;
import com.skipper.base.data.querydsl.PredicateBuilder;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.product.ProductBrandDto;
import com.yitao.client.model.product.ProductBrandModel;
import com.yitao.client.model.product.QProductBrandModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class ProductBrandServices extends GenericEntityService <ProductBrandModel, ProductBrandDto, QProductBrandModel> {
        public ProductBrandServices (EntityManager entityManager){super(entityManager);}

    /**
     *查询品牌列表
     * @param brandCode
     * @param cnName
     * @param enName
     * @return
     */
    public DataOutput<ProductBrandDto> findProductBrandList(String brandCode,String cnName,String enName){
      List<ProductBrandDto> productBrandDtoList = repository.findAll(PredicateBuilder.conjunction()
                .ifHasTextThenAnd(brandCode,()-> Q.brandCode.eq(brandCode))
        .ifHasTextThenAnd(cnName,()->Q.cnName.like("%"+cnName+"%"))
        .ifHasTextThenAnd(enName,() ->Q.enName.like("%"+enName+"%"))
                .get()).stream().map(this::convertEntityToDto).collect(Collectors.toList());
      return new DataOutput(productBrandDtoList);

    }

    /**
     * 根据品牌编码查询品牌信息
     * @param brandCode
     * @return
     */
    public ProductBrandModel findProductBrandByCode(String brandCode){
        if (StringUtils.isNotBlank(brandCode)){
            ProductBrandModel productBrandModel = queryFactory.select(Q).from(Q).where(Q.brandCode.eq(brandCode)).fetchFirst();
            return productBrandModel;
        }
        return null;

    }

}

